Scripting Tutorial: Variables



Variables are what you use to store data values in mIRC like in other programming languages. If you have never heard about Variables here's an analogy: Let's say You have a box labeled A01, another one labeled A02 and a yet another one labeled A03. You've put your favorite novels in box A01, your favorite audio CDs in A02 and your incomplete homeworks in A03.

Now if you were to get someone to get you your favorite CDs you can just tell him to bring you the box labeled A02 instead of making him memorize 50 CD titles. The same would apply for your favorite novels and incomplete homeworks. In this case the boxes are analogous to Variables and the contents in the boxes, to the data values.

Now, there's no rule that you should keep your favorite CDs only in box A02. You can keep them in A01 and your favorite novels in A02 if you want. Only this time you should tell the person to bring box A01. Or you can keep your favorite novels and CDs in box A01 and tell the person to bring the box when you want the two. And the boxes need not be labeled A01, A02 and so on they can be any name you want. And before you can keep you things in the box - you have to create the boxes. In the same way before you can store a value in a Variable you have to create (set) the Variable.

You can actually see where the Variables are stored in mIRC and can edit them right there too. Click on the Variables tab in the mIRC Editor, that's where they are. If you are using some script you must probably will find some entries there. They will look something like the following.

%info this is the new version
%number 23
%date 10/11/2003


The string prefixed with the % is the Variable and everything else after the Variable is it's data value. You can enter a Variable right in the Variables workspace too but the most practical way to do it would be using the set command. You can execute the set command from the commandline or from a script. The following examples set some Variables from the commandline.

/set %song1 Breaking rocks in the hot sun. I fought the law.
/set %song2 He listens to emo but fat Mike is his hero.
/set %item1 apple
/set %item2 mango
/set %item3 orange
/set %item4 banana
/set %item5 sardine
/set %ph 1234567890
/set %e me@mydomain.com

After you set them all look at the Variables workspace, you will find the entries there. Now it's time to see how you can use them. Type the following in the command line and observe. Remember if you want mIRC to evaluate an identifier or a Variable in a command executed from the command line you need to use double front slashes.

//me sings %song1
This will give * Punk-Boi sings Breaking rocks in the hot sun. I fought the law.

//me is now singing %song2
This one will give * Punk-Boi is now singing He listens to emo but fat Mike is his hero.

//say I like %item2 %item3 and %item5
It will make you msg in the active channel or query window "I like mango orange and sardine"

If you are punctuation-cautious you will want to put commas and the fullstop. You can do that with a little modification of the code, change it to //say %item2 $+ , %item3 and %item5 $+ . What happens when you use "%item2,"? (See the comma?) mIRC looks for a Variable named "%item2," which we haven't created. It doesn't find it and gives an error. If you don't remember the use of $+, it's used to concatenate strings.

//say My phone no is: %ph $+ . And my e-mail add is: %e

This will give "My phone no is: 1234567890. And my e-mail add is: me@mydomain.com"

Now go to the Variables panel in the mIRC editor and delete all the Variables we created.

The next example should not be used in mIRC if you are in a cybercafe. It stores your NickServ password as a Variable and using an alias identifies your nick. The process can be made steganographic but that's not what we want to do at the moment.
First set the password with /set %passwd heyho234. Assuming "heyho234" is your password. Next create the alias in Remote.
alias n nickserv identify %passwd
Now all you need to do to identify your nick is type /n.

The same thing can be done with /alias n nisckserv identify heyho234 or better still /alias n ninckserv identify $1 too. Aliases and Variables have their own places in mIRC scripting. Once we start the chapter on Remotes you will start to understand when to use an Alias and when a Variable. If you use an alias in place of a Variable in a situation where it's Variables that best suits, it'd be like taking a truck to go to your kitchen from your bedroom (assuming they are in the same house). I hope you got me.

You will be using the set command mostly from a remote file and not from the command line. In the above example we used it from the command line that was because I just wanted to show you how Variables work basically. When using the set command from a remote file, the command accepts some switches which decides how the command will work. The command is explained below.

set [-snzuN] <%var> [value]

The -snzuN parameters are called switches. They define the options for the set command. I won't be explaining about the switches and other commands related to Variables here because they are already very well documented in the mIRC Help File. Instead I will show you some example scripts which use a lot of Variables and related commands.

The other commands related to Variables are unset, unsetall, inc and dec. Typing /help set in mIRC will take you to the page on Variables and all the commands related to it. The page also explains the various switches for all the commands.

The set command is used to set global Variables, meaning they get entered in the Variables panel and can be called by any other scripts or from the command line. There's another kind of Variable - local, temporary Variables. They exist as long as the routine which created is active and is accessible only by routine which created it. You use the var command to create such Variables. The format for creating such a Variable is var <Variable name> <value>. Eg: var %n 0. Below are two examples using the var command.

alias countDown {
  var %i 0
  while (%i <= 10) {
    echo -a %i
    inc %i
  }
  echo -a Done!
}
This Alias first creates a temporary Variable %i with the value 0. Then it sets a condition "while the value of %i is less than or equal to 10", echo the value of %i and increment it by 1. It goes on till the value of %i becomes 11. The loop condition becomes false and execution proceeds to the next line "echo -a Done!".

Local Variables have global routine scope and no block scope. That means the any Variable created anywhere in a routine can be called from anywhere in the routine but they are not available for other routines even if they are in the same script file. The example below demonstrates it.
alias scopeTest {
  var %t Hey!
  echo -a %t
  {
    var %j Hello!
    echo -a %t
  }
  echo -a %j
  nextRoutine
}

alias nextRoutine {
  echo -a %t
}
When you execute testScope you get the following:
Hey!
Hey!
Hello!
-
* /echo: insufficient parameters (line 13, test.mrc)
-
Analysis:
The variable %t, set in the beginning part of the routine is clearly accessible by a command inside a block in the routine. And the variable %j, set inside the block is as accessible from outside it. But the alias nextRoutine is not able to access the variables declared in the routine (alias scopeTest) which called it. If you want to pass a value to a routine you have to use the value to be passed as an argument to the routine you are calling. It's explained in the Creating Aliases in Remotes section in the chapter on Remotes. You may also like to read more about Custom Identifiers in the chapter on Identifiers.

More examples to demonstrate the use of Variables follows:

The password OP script:

on *:text:*:?: { 
  if (($1 == opmenow) && ($nick != %passop) && ($nick ison #channel)) { 
    set %passop $nick 
    msg $nick Password Accepted! 
    mode #channel +o $nick 
  } 
  else return 
} 
This script first checks if the first word in the message is "opmenow", then it checks if the person's nick name is not already the value of %passop (so that you don't keep OPing the nick over and over again and flood the channel) and finally it checks if the nick is on the channel named #channel. When all the conditions are true, it messages "Password Accepted!" to the nick and then OPs the nick on #channel.

In the example script you must have noticed the ($nick != %passop) condition. It checks if the value of %passop is not (nickname). If you wanted to check if the value of %passop is (nickname), you will have to use ($nick == %passop) .

Here's another example. This one will set three Variables (%jnick, %jchan and %jtime) when someone joins a channel and message in the channel the info about the nick when someone joins next.

on *:join:#: { 
  if (!%jnick) {
    getinfo
  }
  else {
    msg %jchan The last nick to join %jchan was %jnick at %jtime $+ .
    getinfo
  }
}

alias getinfo {
  set %jnick $nick
  set %jchan $chan
  set %jtime $dat at $time $+ .
}
Here's the explanation for the above code: When someone joins a channel the script first checks for the existence of the Variable %jnick. If it's there the rest of the required Variables should be there too and it's not executing for the first time. If %jnick doesn't exist it sets the Variables using an alias named getinfo. If %jnick exists it sends a message to the channel about the nick, channel and time the last nick joined the channel. Also note the use of a custom command or alias to reduce the code length.

Now we will modify the code to check if the nick which joins the channel is the same as the last nick which joined the channel. If true, we'll make mIRC send a message to the channel "Welcome back (nick), the last time you joined the channel was on (date) at (time).".

on *:join:#: {
  if (!%jnick) {
    getinfo
  }
  else {
    if ($nick == %jnick) {
      msg %jchan Welcome back $nick $+ , the last time you joined %jchan was on %jtime $+ ."
      getinfo 
    } 
    else { 
      msg $chan The last nick to join %jchan was %jnick at %jtime $+ . 
      getinfo 
    } 
  } 
} 

alias getinfo { 
  set %jnick $nick 
  set %jchan $chan 
  set %jtime $date at $time 
}
The above example also shows the use of if-else. You will notice another if-else statement is nested under one if-else statement. If you have no clue about if-else read about it in the chapter on Statements first.

The next example contains global and local Variables, If-Else and a While loop. It's a wholesome scripting example. You will need to know about IAL and related Identifiers, Variables and Loops to make sense of the following script. It will scan through the IAL for a given hostname and list all the nicks from that host.

alias scan { 
  set %i 1 
  var %n $nick(#,0) 
  echo -a * SCANNING FOR USERS FROM $1 ... 
  if ($ial(*!*@ $+ [ $1 ] $+ ,0) == 0) { 
    echo -a * No users from $1 $+ . 
    halt 
  } 
  else { 
    while (%i <= %n) { 
      echo -a %i $+ ). $ial(*!*@ $+ [ $1 ] $+ , [ %i ] ).nick 
      inc %i 
    } 
    echo -a * $calc(%i - 1) users from $1 $+ . 
    unset %i 
  } 
} 
This Alias takes one parameter, that's the hostname you want to scan for. For example let's use mirc.com. The script first creates a global Variable named %i and assigns it a value of 1. Next a local Variable named %n with the value of the number of nicks on the channel is created. The Variables being global or local won't make any difference here. I used a global Variable to show you the working of the unset command.

Then you get the "* SCANNING FOR ..." echo. Next comes a very important part: if ($ial(*!*@ $+ [ $1 ] $+ ,0) == 0) {. The executing code would be if $ial(*!*@mirc.com,0). It checks if the number of entries for the hostname mirc.com is 0. If it's true, the script echoes "No users from .." and halts. Else, start the While loop.

while (%i <= %n), in plain English: As long as the value of %i is less than or equal to the number of nicks on the channel. The current value of %i is 1. Then it does an echo for %i $+ ). $ial(*!*@ $+ [ $1 ] $+ , [ %i ] ).nick. The executing code after replacing the variables with their values will be echo -a 1). $ial(*!*@mirc.com,1).nick. $ial(*!*@mirc.com,1).nick returns the first nick from the domain mirc.com. Then the value of %i is increased by one. It goes on till the value of %i is not more then %n, that's the number of nicks on the channel. In effect all the nicks on the channel are scanned.

After the loop finishes the script echoes the number of users found on the channel and then removes the global variable %n with the unset command.

In the above examples you have seen == and <= used within the If statement to evaluate a condition. They are called Operators. There are many more Operators in mIRC and are frequently used in more complex scripts. The next chapter deals with them.


Back | Table of Contents | Operators



Copyright © 2002-2004 SpyderWares.
Feel free to distribute this tutorial in part or whole, just make sure the credits stay intact.

http://spyderwares.com